//
// Created by Scave on 2025/9/1.
//

#ifndef RIDER3D_LAZY_H
#define RIDER3D_LAZY_H

#include "Define.h"

NS_RIDER_BEGIN

    template<typename T>
    class Lazy {
    public:
        static Lazy<T> Create(FUNCTION<T()> factory, FUNCTION<bool(T&)> validator) {
            return Lazy<T>(factory, validator);
        }

        static Lazy<T> Create(FUNCTION<T()> factory) {
            return Lazy<T>(factory, [](T& data) {
                return data != nullptr;
            });
        }

        T Get() {
            if (!validator(value)) {
                value = factory();
            }
            return value;
        }
    private:
        Lazy(FUNCTION<T()> factory, FUNCTION<bool(T&)> validator) {
            this->factory = factory;
            this->validator = validator;
        }

        T value;
        FUNCTION<T()> factory;
        FUNCTION<bool(T&)> validator;
    };

NS_RIDER_END

#endif //RIDER3D_LAZY_H
